#include <iostream>
#define LL long long
using namespace std;

const int kMaxN = 1024;
const int kMaxM = 1024;
const LL kInf = 1e18;

int a[kMaxN][kMaxM];
LL f[kMaxN][kMaxM];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      f[i][j] = -kInf;
    }
  }

  f[0][0] = 0;
  for (int i = 1; i < n; i++) {
    f[i][0] = -kInf;
  }

  for (int j = 1; j <= m; j++) {
    LL sum = 0, tmp = -kInf;

    for (int i = 0; i < n; i++) {
      tmp = max(tmp, f[i][j - 1] - sum);
      sum += a[i][j];
      f[i][j] = max(f[i][j], tmp + sum);
    }

    sum = 0, tmp = -kInf;
    for (int i = n - 1; i >= 0; i--) {
      tmp = max(tmp, f[i][j - 1] - sum);
      sum += a[i][j];
      f[i][j] = max(f[i][j], tmp + sum);
    }
  }
  cout << f[n - 1][m] << "\n";
  return 0;
}